<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const loading = ref(false)
const purchaseList = ref([])
const purchasePage = ref(1)
const purchasePageSize = ref(10)
const purchaseTotal = ref(0)
const purchaseDetailDialog = ref(false)
const purchaseDetailList = ref([])

const getPurchaseList = async () => {
  loading.value = true
  try {
    const res = await axios.get('/carRepair/admin/partManage/purchaseRecordPage', {
      params: {
        pageNum: purchasePage.value,
        pageSize: purchasePageSize.value
      }
    })
    if (res.data.code === 200) {
      console.log(res.data.data.records)
      purchaseList.value = res.data.data.records
      purchaseTotal.value = res.data.data.total
    }
  } catch (e) {
    ElMessage.error('获取采购记录失败')
  } finally {
    loading.value = false
  }
}

const showPurchaseDetail = async (row) => {
  try {
    const res = await axios.get(`/carRepair/admin/partManage/purchaseOrderDetail/${row.orderId}`)
    if (res.data.code === 200) {
      purchaseDetailList.value = res.data.data
      purchaseDetailDialog.value = true
    }
  } catch (e) {
    ElMessage.error('获取采购明细失败')
  }
}

onMounted(() => {
  getPurchaseList()
})
</script>

<template>
  <div class="purchase-record">
    <el-card>
      <el-table :data="purchaseList" style="width: 100%" v-loading="loading">
        <el-table-column prop="orderId" label="采购单ID" width="180" />
        <el-table-column prop="quantity" label="采购数量" width="150" />
        <el-table-column prop="totalAmount" label="采购总金额" width="150">
          <template #default="scope">¥{{ scope.row.totalAmount }}</template>
        </el-table-column>
        <el-table-column prop="purchaseDate" label="采购日期" width="200" />
        <el-table-column label="详情" width="100">
          <template #default="scope">
            <el-button type="success" size="small" @click="showPurchaseDetail(scope.row)">明细</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          v-model:current-page="purchasePage"
          v-model:page-size="purchasePageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="purchaseTotal"
          @size-change="getPurchaseList"
          @current-change="getPurchaseList"
        />
      </div>
    </el-card>

    <!-- 采购明细弹窗 -->
    <el-dialog v-model="purchaseDetailDialog" title="采购明细" width="60%">
      <el-table :data="purchaseDetailList" style="width: 100%">
        <el-table-column label="零件图片" width="120">
          <template #default="scope">
            <el-image style="width: 80px; height: 80px" :src="scope.row.imageUrl" :preview-src-list="[scope.row.imageUrl]" fit="cover" />
          </template>
        </el-table-column>
        <el-table-column prop="supplierName" label="供应商" />
        <el-table-column prop="partTypeName" label="零件类型" />
        <el-table-column prop="partName" label="零件名称" />
        <el-table-column prop="quantity" label="采购数量" />
        <el-table-column prop="unitPrice" label="采购单价">
          <template #default="scope">¥{{ scope.row.unitPrice }}</template>
        </el-table-column>
        <el-table-column prop="totalAmount" label="采购总价">
          <template #default="scope">¥{{ scope.row.totalAmount }}</template>
        </el-table-column>
        <el-table-column prop="purchaseDate" label="采购日期" width="160" />
      </el-table>
    </el-dialog>
  </div>
</template>

<style scoped>
.purchase-record {
  padding: 0 0;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>